const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const userCollection = db.collection('users')

exports.main = async (event, context) => {
  const { username, password } = event
  
  try {
    // 从数据库中查询用户
    const { data } = await userCollection.where({
      username: username,
      password: password  // 实际项目中应该使用加密密码
    }).get()
    
    if (data.length > 0) {
      return {
        success: true,
        msg: '登录成功',
        userInfo: data[0]
      }
    }
    
    return {
      success: false,
      msg: '用户名或密码错误'
    }
    
  } catch (err) {
    return {
      success: false,
      msg: '登录失败',
      error: err
    }
  }
} 